package com.talkweb.demo.search;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wangshaopeng@talkweb.com.cn
 * @desccription 在此输入描述
 * @date 2024/10/10
 */
public class FileCacheUtil {
    private static final Set<String> fileNameSet = new HashSet<String>();
    private static File cacheFile;

    public static void init(String cacheFileName){
        cacheFile = new File(cacheFileName);
        if(cacheFile.exists()){
            FileReader fileReader = FileReader.create(cacheFile);
            List<String> lines = fileReader.readLines();
            fileNameSet.addAll(lines);
        }
    }

    public static boolean cache(String value) {
        FileWriter fileWriter = FileWriter.create(cacheFile);
        fileWriter.append(value+"\r\n");
        fileNameSet.add(value);
        return true;
    }

    public static boolean exists(String value) {
        return fileNameSet.contains(value);
    }
}
